﻿function requestFee(requestUrl,optType){
	$.ajax({
		type:"post",
		url:requestUrl,
		success:function(resultData){
			var flag = resultData.success;	
          	console.log(" flag = "+flag);		
			if(flag==1){
				var fee=resultData.data;			
				console.log("fee:"+fee.length);
				var str="";
				var timeArray= new Array();	
				var tatalArray= new Array();
				var baoyueArray= new Array();
				var dianbaoArray= new Array();	
				
				for(var i=0;i<fee.length;i++){					
					var feeData=fee[i];					
					var vDate= feeData.time;
					var vTotal= feeData.total_fee;
					var vBaoyue=feeData.baoyue_fee;
					var vDianBao=feeData.dianbo_fee;
					
					timeArray[i]=vDate;
					tatalArray[i]=vTotal;
					baoyueArray[i]=vBaoyue;
					dianbaoArray[i]=vDianBao;					
					var strData;
					if( optType==1 ){
						strData=vDate.slice(0,4)+"."+vDate.slice(4,6)+"."+vDate.slice(6,8);
					}else if( optType==2 ){
						strData=vDate.slice(0,4)+"年"+vDate.slice(4,6)+"月";
					}
					var strTotal= Math.floor(vTotal/10000)+'w';
                    var  strBaoyue= Math.floor(vBaoyue/10000)+'w';
					var  strDianbao=Math.floor(vDianBao/10000)+'w';
					str+='<tr><td>'+strData+'</td>  <td>'+strTotal+'</td> <td>'+strBaoyue+'</td><td>'+strDianbao+'</td></tr>';
				}
				drawChart2(timeArray,tatalArray,baoyueArray,dianbaoArray,optType);
				$("#fee-table-body").html(str);	
			}			
		}
	});
}

window.onload= function (){
    var thisURL = document.URL; 
    console.log("thisURL="+thisURL);	
	var optType=getOptType(thisURL);	 
	console.log("optType="+optType);	
	setTitle(optType);
	requestIncomeData(optType);	
}

function getOptType(thisUrl){
	  var  value =thisUrl.split('?')[1];  
	  var  optType= value.split('=')[1];  	
	  return optType;
}
function setTitle(optType){
	if(optType=='1'){
		document.getElementById('s_title').innerHTML='昨日累计总收入';
	}else if(optType=='2'){
		document.getElementById('s_title').innerHTML='本月累计总收入';
	}
}

function requestIncomeData(optType){
	var requestUrl;
	console.log("requestUrl:"+requestUrl);
	if(optType=='1'){
		requestUrl=getIncomeDailyRequestUrl();
	}else if(optType=='2'){
		requestUrl=getIncomeMonthlyRequestUrl();
	}
	if(requestUrl!=undefined){
		requestFee(requestUrl,optType);
	}
}

function 	drawChart2(timeArray,tatalArray,baoyueArray,dianbaoArray,optType){
	
	var tatal=new Array();
	var baoyue= new Array();
	var dianbao= new Array();

	var maxTotal;
	if(tatalArray.length>0){		
		maxTotal=Number(tatalArray[0]);
	}
	console.log("optType="+optType);
   	for(var i=0;i<tatalArray.length;i++){
		var vDate=timeArray[i];
		var strDateFormat;
		if(optType=='1'){
			strDateFormat=vDate.slice(6,8)+"日";
		}else if(optType=='2'){
			strDateFormat= vDate.slice(4,6);		
			console.log(vDate);			
			if(strDateFormat.slice(0,1)=='0'){
				strDateFormat=strDateFormat.slice(1,strDateFormat.length)+"月";
			}else{
				strDateFormat=strDateFormat+"月";
			}
			console.log("strDateFormat="+strDateFormat);
		}
		var vTotal=tatalArray[i];
	    var vBaoyue=baoyueArray[i];
		var vDianBao=dianbaoArray[i];
		//data to the chart array				
		tatal[i]=[strDateFormat,vTotal/10000];
		baoyue[i]=[strDateFormat,vBaoyue/10000];
		dianbao[i]=[strDateFormat,vDianBao/10000];
		
		//find the Y max value
		var numberTotal= Number(vTotal);		
		if(numberTotal>maxTotal){
			maxTotal=numberTotal;
		}				
	}	
    if(maxTotal!=undefined){
		maxTotal=(Math.floor(maxTotal/(100*10000))+1)*100;		
	}	
	drawChart(tatal,baoyue,dianbao,maxTotal,0);
}


function drawChart(tatalArray,baoyueArray,dianbaoArray,yMax,yMin){
	
	for(var i=0;i<tatalArray.length;i++){
		var t=tatalArray[i];
		var b=baoyueArray[i];
		var d=dianbaoArray[i];
		console.log("t="+t+",b="+b+",d="+d);
	}
	
	var myChart = new JSChart('income-chart', 'line');	
	myChart.setDataArray(tatalArray, 'red');
	myChart.setDataArray(baoyueArray, 'green');
	myChart.setDataArray(dianbaoArray, 'gray');	
	myChart.setLineColor('#ff0000', 'red');
	myChart.setLineColor('#00ff00', 'green');
	myChart.setLineColor('#BBBBBB', 'gray');
	
	myChart.setAxisValuesPaddingLeft(50);
	myChart.setAxisValuesNumberY(5);
	myChart.setIntervalStartY(yMin);
	myChart.setIntervalEndY(yMax);
	
	for(var i=0;i<tatalArray.length;i++){
		myChart.setTooltip([i]);
		myChart.setFlagColor('#9D16FC');
		myChart.setFlagRadius(4);
	}
	myChart.setTitle("");
	myChart.setSize(360,197);
	myChart.draw();
}